package com.jiangxjsy.admin.service.impl;

import com.jiangxjsy.admin.mapper.AdminMenuMapper;
import com.jiangxjsy.admin.service.AdminMenuService;
import com.jiangxjsy.admin.service.AdminUserRoleService;
import com.jiangxjsy.core.system.SystemCore;
import com.jiangxjsy.model.admin.entity.AdminMenu;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * wukong-crm-master
 * 蒋胜宇
 * 2025/6/6 16:46
 */

@Service
public class AdminMenuServiceImpl implements AdminMenuService {
    @Resource
    private AdminMenuMapper adminMenuMapper;

    @Resource
    private AdminUserRoleService adminUserRoleService;

    /**
     * 查询用户所拥有的菜单权限
     *
     * @param userId 用户 id
     * @return 菜单列表
     */
    @Override
    public List<AdminMenu> queryMenuList(Long userId) {
        if (SystemCore.getSuperUser().equals(userId) ||
                adminUserRoleService.queryUserRoleIds(userId).contains(SystemCore.getSuperRole())
        ) {
            return adminMenuMapper.selectAll();
        }
        return adminMenuMapper.selectMenuList(userId);
    }

    /**
     * 查询不在所提供的菜单列表中的其它菜单
     *
     * @param menuIdList 菜单 id 列表
     * @return 菜单列表
     */
    @Override
    public List<AdminMenu> queryNoAuthMenu(List<Long> menuIdList) {
        return adminMenuMapper.selectNoAuthMenu(menuIdList);
    }

    /**
     * 查询所有菜单
     *
     * @return 菜单列表
     */
    @Override
    public List<AdminMenu> queryAll() {
        return adminMenuMapper.selectAll();
    }

    /**
     * 查询公海菜单权限
     *
     * @param userId 用户 id
     * @param deptId 部门 id
     * @return 菜单权限
     */
    @Override
    public Map<String, Long> queryPoolReadAuth(Long userId, Long deptId) {
        return adminMenuMapper.selectPoolReadAuth(userId, deptId);
    }

    /**
     * 根据父级 id 和权限标识查询菜单 id
     *
     * @param parentId 父级 id
     * @param realm 权限标识
     * @return 菜单 id
     */
    @Override
    public Integer queryMenuIdByParentIdAndRealm(Long parentId, String realm) {
        return adminMenuMapper.selectMenuIdByParentIdAndRealm(parentId, realm);
    }
}
